#include "Scroll.h"

; Scroll::Scroll(){
	Posicion* pos = new Posicion(820,10);
	this->panelScroll= new Rectangulo("panel",70,600,pos);
	this->crearBotones();
	this->crearObstaculos();
	this->limiteInferior = 30;
	this->limiteSup = 34;
	this->tope = this->sizeListaObstaculos()*4;
}

void Scroll::crearObstaculos(){
	Posicion* pos1 = new Posicion(830,75);
	Rectangulo* obj1 = new Rectangulo("obj1",50,50,pos1);
	obj1->setIdTextura("Images/agua.bmp");
	this->listaObstaculos.push_back(obj1);

	Posicion* pos2 = new Posicion(830,140);
	Rectangulo* obj2 = new Rectangulo("obj2",50,50,pos2);
	obj2->setIdTextura( "Images/muro.bmp");
	this->listaObstaculos.push_back(obj2);

	Posicion* pos3 = new Posicion(830,205);
	Rectangulo* obj3 = new Rectangulo("obj3",50,50,pos3);
	obj3->setIdTextura("Images/ladrillos.bmp");
	this->listaObstaculos.push_back(obj3);

	Posicion* pos4 = new Posicion(830,270);
	Rectangulo* obj4 = new Rectangulo("obj4",50,50,pos4);
	obj4->setIdTextura( "Images/bosque.bmp");
	this->listaObstaculos.push_back(obj4);

	Posicion* pos5 = new Posicion(830,335);
	Rectangulo* obj5 = new Rectangulo("obj5",50,50,pos5);
	obj5->setIdTextura( "Images/hielo.bmp");
	this->listaObstaculos.push_back(obj5);

	Posicion* pos6 = new Posicion(830,400);
	Rectangulo* obj6 = new Rectangulo("obj6",50,50,pos6);
	obj6->setIdTextura( "Images/fuego.bmp");
	this->listaObstaculos.push_back(obj6);

	Posicion* pos7 = new Posicion(830,465);
	Rectangulo* obj7 = new Rectangulo("obj7",50,50,pos7);
	obj7->setIdTextura( "Images/arena.bmp");
	this->listaObstaculos.push_back(obj7);

	Posicion* pos8 = new Posicion(830,530);
	Rectangulo* obj8 = new Rectangulo("obj8",50,50,pos8);
	obj8->setIdTextura( "Images/barro.bmp");
	this->listaObstaculos.push_back(obj8);

	Posicion* pos9 = new Posicion(830,595);
	Rectangulo* obj9 = new Rectangulo("obj9",50,50,pos9);
	obj9->setIdTextura( "Images/tank.bmp");
	this->listaObstaculos.push_back(obj9);
}

void Scroll::crearBotones(){
	Posicion* pos1 = new Posicion(820,560);
	this->botonDown = new Rectangulo("botDown",70,50,pos1);
	this->botonDown->setIdTextura("Images/down.bmp");

	Posicion* pos2 = new Posicion(820,10);
	this->botonUp = new Rectangulo("botup",70,50,pos2);
	this->botonUp->setIdTextura("Images/up.bmp");
}

int Scroll::getLimiteSup(){
	return this->limiteSup;
}

int Scroll::getLimiteInf(){
	return this->limiteInferior;
}
	
void Scroll::setLimiteSup(int limSup){
	this->limiteSup = limSup;	
}


void Scroll::setLimiteInf(int limInf){
	this->limiteInferior = limInf;	
}

int Scroll:: getTope(){
	return this->tope;
}

void Scroll::dibujar(SDL_Surface* screen){
	this->panelScroll->dibujar(screen);

	Rectangulo *obstaculo;
	int i = 1;
	std::list<Rectangulo*>::iterator iter;
	iter = this->iteratorListaObstaculos();
	
	while(i<= this->sizeListaObstaculos()){
		obstaculo = *iter;
		if(estaDentroDelScroll(obstaculo)){
		obstaculo->dibujar(screen);
		};
		i++;
		iter++;
	}
	this->botonDown->dibujar(screen);
	this->botonUp->dibujar(screen);

	int coordX = this->panelScroll->getPosicion()->getX();
	int coordY = this->panelScroll->getPosicion()->getY();
	int ancho = this->getPanelScroll()->getBase();
	int alto = this->getPanelScroll()->getAltura();
	SDL_UpdateRect(screen, coordX, coordY, ancho, alto);

}
bool Scroll::estaDentroDelScroll(Rectangulo * obstaculo){
	//compruebo que este dentro de los margenes del panel
	int techo = this->panelScroll->getPosicion()->getY();
	int piso = this->panelScroll->getPosicion()->getY() + this->panelScroll->getAltura()- obstaculo->getAltura();

	if ((obstaculo->getPosicion()->getY()>= techo)&&
		(obstaculo->getPosicion()->getY()<= piso)){
		std::cout <<"esta dentro del scroll"<< std::endl;
				return true;
	}
	else{std::cout <<"No esta dentro del scroll"<< std::endl;
		return false;};
}

std::list<Rectangulo*>::iterator Scroll::iteratorListaObstaculos(){
	return this->listaObstaculos.begin();
}

Rectangulo* Scroll::getBotonDown(){
	return this->botonDown;
}

Rectangulo* Scroll::getBotonUp(){
	return this->botonUp;	
}
	
void Scroll::actualizarPosiciones(int y){
	Rectangulo *rect;
	int i = 1;
	std::list<Rectangulo*>::iterator iter;
	iter = this->iteratorListaObstaculos();
	while(i <= this->sizeListaObstaculos()){
		rect = *iter;
		rect->setPosicion(rect->getPosicion()->getX(), rect->getPosicion()->getY()+y);
		i++;
		iter++;
	}
}

int Scroll::sizeListaObstaculos(){
	return this->listaObstaculos.size();
}

Rectangulo* Scroll::getPanelScroll(){
	return this->panelScroll;
}
Scroll::~Scroll(){
	Rectangulo *rect;
	int i = 1;
	std::list<Rectangulo*>::iterator iter;
	iter = this->iteratorListaObstaculos();
	while(i <= this->sizeListaObstaculos()){
		rect = *iter;
		delete rect;
		i++;
		iter++;
	}
	delete this->botonDown;
	delete this->botonUp;
	delete this->panelScroll;
}
